import {HTTP} from "../util/http-book.js"

class KeywordModel extends HTTP{
  key = 'q'
  maxLength = 10
  // 拉去缓存中所有热门搜索
  getHistory(){
    const words = wx.getStorageSync(this.key)
    if (!words){
      return []
    }
    return words
  }
  
  getHot(){
    return this.request({
      url:'book/hot_keyword'
    })
  }

  // 写入所有搜索关键词
  addToHistory(keyword){
    let words = this.getHistory()
    let has = words.includes(keyword)
    if(!has){
      const length = words.length
      //删除末尾数据 
      if(length >= this.maxLength){
        words.pop()
      }
      words.unshift(keyword)
      wx.setStorageSync(this.key, words)
    }
  }
}

export { KeywordModel}